package cn.edu.hatu.bookshop.controller;

import cn.edu.hatu.bookshop.entity.Book;
import cn.edu.hatu.bookshop.entity.Classify;
import cn.edu.hatu.bookshop.entity.Customer;
import cn.edu.hatu.bookshop.service.BookService;
import cn.edu.hatu.bookshop.service.ClassifyService;
import cn.edu.hatu.bookshop.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class CustomerController {
    private BookService bookService;
    private ClassifyService classifyService;
    private CustomerService customerService;

    @Autowired
    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    @Autowired
    public void setClassifyService(ClassifyService classifyService) {
        this.classifyService = classifyService;
    }

    @Autowired
    public void setBookService(BookService bookService) {
        this.bookService = bookService;
    }


    @RequestMapping("/index")
    public String index() {
        return "index";
    }
    //导航栏 ：书籍分类
    @RequestMapping("/ClassifyBooks")
    public String showBooks(Model model,HttpServletRequest request) {
        int classifyId =1;
        if(request.getParameter("classifyId")!=null){
            classifyId = Integer.parseInt(request.getParameter("classifyId"));
        }
        List<Book> books = bookService.getBooksByClassifyId(classifyId);
        model.addAttribute("books", books);
        String classifyName = classifyService.getClassifyName(classifyId);
        model.addAttribute("classifyName",classifyName);
        return "Customer/ShowBook/ShowBooks";
    }

    @RequestMapping("/showBookDetail")
    public String showBookDetail(Model model, HttpServletRequest request){
        int bookId = Integer.parseInt(request.getParameter("bookId"));
        Book book = bookService.findById(bookId);
        model.addAttribute("book",book);
        String classifyName = classifyService.getClassifyName(book.getClassifyId());
        model.addAttribute("classifyName",classifyName);
        return "Customer/ShowBook/ShowBookDetail";
    }
    @RequestMapping("/changePwdInput")
    public String changePassword(Model model,HttpServletRequest request){
        Cookie[] cookies = request.getCookies();//获取cookie数组
        int customerId = 0;
        for (Cookie c:cookies   //遍历cookie
        ) {
            if (c.getName().equals("customerId")) {
                customerId = Integer.parseInt(c.getValue());
                break;
            }
        }
        if(customerId==0){
            return "redirect:myCart";
        }else{
            String oldPassword = "";
            for (Cookie c:cookies   //遍历cookie
            ) {
                if (c.getName().equals("oldPassword")) {
                    System.out.println("getValue:"+c.getValue());
                    oldPassword=c.getValue();
                    System.out.println("oldPassword:"+oldPassword);
                    break;
                }
            }
            model.addAttribute("oldPassword",oldPassword);
            return "Customer/Site/ChangePassword";
        }

    }
    @RequestMapping("/changePwd")
    public String changePwd(HttpServletRequest request){
        String newPassword = request.getParameter("newPassword");
        Cookie[] cookies = request.getCookies();//获取cookie数组
        for (Cookie c:cookies   //遍历cookie
        ) {
            if (c.getName().equals("customerId")) {
                int customerId = Integer.parseInt(c.getValue());
                customerService.changePassword(newPassword,customerId);
                break;
            }
        }
        return "redirect:homePage";
    }
    @RequestMapping("/showProfile")
    public String showProfile(Model model,HttpServletRequest request){
        int customerId = 0;
        Cookie[] cookies = request.getCookies();//获取cookie数组
        Customer customer = new Customer();
        for (Cookie c:cookies   //遍历cookie
        ) {
            if (c.getName().equals("customerId")) {
                customerId = Integer.parseInt(c.getValue());
                customer = customerService.findById(customerId);
                break;
            }
        }
        if(customerId==0){
            return "redirect:myCart";
        }else{
            model.addAttribute("customer",customer);
            return "Customer/Site/CustomerProfile";
        }
    }

}
